package com.pinganfu.mock.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;  
import java.io.ObjectInputStream;  
import java.io.ObjectOutputStream;  
import java.io.RandomAccessFile;
import java.net.ServerSocket;  
import java.net.Socket;  
import java.util.ArrayList;  
import java.util.List;  

import org.json.JSONObject;
import org.json.XML;
  
public class ObjectServerApp extends Thread {  
  
    private Socket client;  
  
    /* 
     * 构造函数，接收每一个socket实例 
     */  
    public ObjectServerApp(Socket socket) {  
        this.client = socket;  
    }  
  
    /* 
     * 线程执行方法 
     */
    public void run() {  
        try {  
            System.out.println("线程开启");  
            //对象数据流的获取与返回  
            ObjectInputStream in = new ObjectInputStream(client.getInputStream());  
            ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());  
            
            
            
            City city = (City) in.readObject();  
            List cityList = city.getCityList();  
            for (int i = 0; i < cityList.size(); i++) {  
                System.out.println("服务器端得到的数据：" + cityList.get(i).toString());  
            }  
            List list = new ArrayList();  
            list.add("广州");  
            list.add("深圳");  
            City cityBack = new City();  
            cityBack.setCityList(list);  
            out.writeObject(cityBack); 
            in.close();  
            out.close();  
        } catch (Exception e) {  
        } finally {  
        }  
    } 
    
    
    /*public void run() {  
    	BufferedWriter buffWri=null;  
    	String jsString = "";
        try {  
        	
            System.out.println("线程开启");  
            //对象数据流的获取与返回  
            ObjectInputStream in = new ObjectInputStream(client.getInputStream());  
            ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());  
            
            String json =  (String) in.readObject();  
            System.out.println("服务器端得到的数据：" + json.toString());  
            
            String fileName2="json.xml";    
            buffWri=new BufferedWriter(new FileWriter(fileName2));    
            JSONObject jsonObj=new JSONObject(json);    
            buffWri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            buffWri.newLine();    
            buffWri.write("<ROOT>");
            buffWri.newLine();
            buffWri.write(XML.toString(jsonObj));
            buffWri.newLine();    
            buffWri.write("</ROOT>");    
            buffWri.newLine();    
            buffWri.flush();    
            
            final RandomAccessFile randomFile = new RandomAccessFile(fileName2,"rw");
            String tmp = "";     
            while( (tmp = randomFile.readLine())!= null) {     
                System.out.println(new String(tmp.getBytes("ISO8859-1")));     
            }     
            jsString = new String(tmp.getBytes("ISO8859-1"));
            
            System.out.println(jsString);
            
            out.writeObject(jsString);
            out.flush();
            in.close();  
            out.close();  
        } catch (Exception e) {  
        } finally {  
        }  
    }
    */
  
    /* 
     * 启用线程方法实现多个用户连接socket服务器端. 
     */  
    public static void main(String[] args) throws IOException {  
        // 创建服务端，监听端口6688  
        ServerSocket server = new ServerSocket(6688);  
        System.out.println("服务器已经开启，等待连接。");  
        // 使用服务器端保持永久监听状态  
        while (true) {  
            // 接收每一个客户端的连接，并返回socket实例  
            ObjectServerApp osa = new ObjectServerApp(server.accept());  
            // 为每一个客户端启一个线程，去执行操作  
            osa.start();  
        }  
    }  
}  
